'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 幸运宝箱
 * @author sundbcn@gmail.com (sun debing)
 */

var dataApi = require('../libs/dataApi');
var _ = require('lodash');

/**
 * 获取配置
 * @param  {[type]} vip [description]
 * @return {[type]}     [description]
 */
exports.getConf = function (box_id) {
    var conf = _.clone(dataApi.lucky_box.findById(box_id));
    return conf;
};

/**
 * 取品质item
 * @param  {[type]} vip [description]
 * @return {[type]}     [description]
 */
exports.getQualityItem = function (box_id, quality) {
    var total_conf = _getConfig(box_id);
    var all_quality_list_out = _getQualityList(total_conf)[quality];
    return all_quality_list_out;
};

/**
 * 取保底奖励
 * @param  {[type]} vip [description]
 * @return {[type]}     [description]
 */
exports.luckyList = function (box_id) {
    var conf = this.getConf(box_id);
    if (!conf) {
        console.error('lucky_conf type can not found:' + box_id);
        return;
    }
    var total_end_list = [];
    var total_num = conf.num;
    var save_list = _.clone(_contItemNum(_getSaveReward(box_id)), true);
    var end_num = total_num - save_list.length;
    // 取保底次数后，剩余奖励
    //for (var i = 0; i < end_num; i++) {
    //save_list.push(_probability(box_id));
    //}

    //_addControlNum(save_list, box_id);

    // jshint maxdepth: 10
    var iii = 0;
    if (box_id === 3) {
        while (save_list.length <= end_num) {
            iii++;
            if (!!_addControlNum(save_list, box_id)) {
                save_list.push(_probability(box_id));
            }
            if (iii > 3000) {
                var end_save_num = total_num - save_list.length;
                for (i = 0; i < end_save_num; i++) {
                    var total_conf = _getConfig(box_id);
                    var all_quality_list_out = _getQualityList(total_conf)[2];
                    var random_num = _.random(0, all_quality_list_out.length - 1);
                    save_list.push(all_quality_list_out[random_num]);
                }
                break;
            }
        }
    } else {
        for (var i = 0; i < end_num; i++) {
            save_list.push(_probability(box_id));
        }
    }
    return save_list;
};

// 增加品质个数控制
var _addControlNum = function (list, box_id) {
    var state = true;
    var max_limit_list = _getBoxForm(box_id);
    _.each(max_limit_list, function (max_item) {
        if (_contNum(list)[max_item.quality] > max_item.max_num) {
            state = false;
        }
    });
    return state;
};

// 品质个数
var _contNum = function (list_all) {
    var avg_user_count = {};
    var list = [];
    _.forEach(list_all, function (item) {
        if (!avg_user_count[item.quality]) {
            avg_user_count[item.quality] = 1;
        } else {
            avg_user_count[item.quality]++;
        }
    });
    return avg_user_count;
};
// 随机取奖励道具
var _probability = function (box_id) {
    var total_probability = 0;
    var list = [];
    var reward_list_all = [];
    var quality = 0;
    var conf = _getBoxForm(box_id);

    _.each(conf, function (probability_item) {
        var probability_item1 = _.clone(probability_item, true);
        total_probability += probability_item1.probability;
        probability_item1.total_probability = total_probability;
        list.push(probability_item1);
    });
    var random_num = _.random(0, total_probability - 1);
    _.some(list, function (reward_item) {
        if (reward_item.total_probability > random_num) {
            quality = reward_item.quality;
            return true;
        }
    });

    // 取随机出来的品质奖励
    var reward_all_list = _getConfig(box_id);

    _.each(reward_all_list, function (quality_item) {
        var quality_item1 = _.clone(quality_item, true);
        if (quality_item1.quality == quality) {
            reward_list_all.push(quality_item1);
        }
    });

    // 取出随机item
    var random_num1 = _.random(0, reward_list_all.length - 1);
    return reward_list_all[random_num1];
};

// 取保底奖励
var _getSaveReward = function (box_id) {
    var save_all_conf = _getBoxForm(box_id);
    var handle_list = [];
    var all_list = [];

    // 保底conf操作
    /* jshint maxstatements:150 */
    var total_conf = _getConfig(box_id);
    var all_quality_list_out = _getQualityList(total_conf);

    _.each(save_all_conf, function (save_all_conf_item) {
        var num = save_all_conf_item.num;
        if (!num) {
            return true;
        }

        var quality = save_all_conf_item.quality;

        // 取对应品质的conf
        var all_quality_list = all_quality_list_out[quality];

        if (quality == 4 && box_id == 3) {
            _.each(all_quality_list, function (soldier_item) {
                if (soldier_item.reward_type == 25) {
                    handle_list.push(soldier_item);
                }
            });
            for (var i = 0; i < num; i++) {
                var quality2_random2 = _.random(0, handle_list.length - 1);
                all_list.push(handle_list[quality2_random2]);
            }
        } else {
            for (var ii = 0; ii < num; ii++) {
                var quality2_random = _.random(0, all_quality_list.length - 1);
                all_list.push(all_quality_list[quality2_random]);
            }
        }
    });
    return all_list;
};

// 取对应品质的列表
var _getQualityList = function (quality_list) {
    if (quality_list.length < 1) {
        console.error('luckyBox not reward  conf');
        return;
    }

    var quality_item = {};
    var quality_list_all = [];

    _.each(quality_list, function (all_quality_item) {
        if (!quality_item[all_quality_item.quality]) {
            quality_item[all_quality_item.quality] = [];
        }
        quality_item[all_quality_item.quality].push(all_quality_item);
    });

    return quality_item;
};

// 是否取出全部奖励(返回总个数)
var _totalReward = function (total_list) {
    var num = 0;
    _.each(total_list, function (total_list_item) {
        num += total_list_item.reward_num;
    });
    return num;
};

// 统计个数（相同的累加）
var _contItemNum = function (list_all) {
    var avg_user_count = {};
    var list = [];
    _.forEach(list_all, function (item) {
        if (!avg_user_count[item.reward]) {
            avg_user_count[item.reward] = _.clone(item, true);
        } else {
            avg_user_count[item.reward].reward_num += item.reward_num;
        }
    });
    _.forEach(avg_user_count, function (value) {
        list.push(value);
    });
    return list;
};

var _getConfig = function (box_id) {
    var list = [];
    list = _.clone(dataApi.lucky_box_config.findById(box_id), true);
    return list;
};

var _getBoxForm = function (box_id) {
    var list = [];
    list = _.clone(dataApi.lucky_box_form.findById(box_id), true);
    return list;
};
